const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    upImg:'',
    upImgId:'',
    startDate: "",
    date: '请选择截止时间',
    userName:'',
    uid:'',
    caimVal:'',
    descriptionVal:'',
    contactVal:'',
    budgetVal:'',
    serviceId:'',
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDate()
    if (options.type == 2){
      this.setData({
        type: options.type,
        serviceId: options.id
      })
    }
      this.setData({
        userName: options.userName,
        uid: options.uid
      })


  },
  // 上传图片
  upImgBtn:function(){
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFilePaths[0]
          },
          success(upres) {
            wx.hideLoading()
            var obj = JSON.parse(upres.data)
            if (obj.code == 1000) {
              _this.setData({
                upImg: obj.data.url,
                upImgId: obj.data.id
              })
            }
          }
        })
      }
    })
  },

  getDate(){
    let day3 = new Date();
    day3.setTime(day3.getTime()+24*60*60*1000);
    let startDate = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
    this.setData({
      startDate
    })
  },

  // 删除图片
  delImg:function(){
    this.setData({
      upImg:''
    })
  },
  // 选中时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 想要做的
  caimInp:function(e){
    this.setData({
      caimVal: e.detail.value
    })
  },
  // 描述
  descriptionInp: function (e) {
    this.setData({
      descriptionVal: e.detail.value
    })
  },
  // 联系方式
  contactInp: function (e) {
    this.setData({
      contactVal: e.detail.value
    })
  },
  // 预算
  budgetInp: function (e) {
    this.setData({
      budgetVal: e.detail.value
    })
  },
  // 提交
  submitBtn:function(){
    var _this = this;
    var { caimVal, descriptionVal, contactVal, budgetVal, upImgId, uid, date, userName, serviceId, type} = this.data;
    if (caimVal == '' || descriptionVal == '' || contactVal == '' || budgetVal == ''){
      wx.showToast({
        title: '您的信息还未填写完整',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (date == '请选择截止时间'){
      wx.showToast({
        title: '请选择时间',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (type != 2){
      serviceId = ''
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/createEmploy',
      data: {
        token: app.globalData.token,
        title: caimVal,
        desc: descriptionVal,
        phone: contactVal,
        delivery_deadline: date,
        bounty: budgetVal,
        employee_uid: uid,
        file_id: upImgId,
        service_id: serviceId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '/taskPage/pages/hirePay/hirePay?price=' + budgetVal + '&title=' + userName + '&code=' + res.data.data.orderCode + '&orderid=' + res.data.data.order_id + '&employid=' + res.data.data.employ_id,
            })
          },2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
